package com.sc.service.Vast_service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.Vast_pojo.domain.DeviceGroup;
import com.sc.Vast_pojo.dto.DeviceGroupPageQueryDTO;
import com.sc.dto.DeviceGroupUpdateReqDTO;
import com.sc.exception.DeletionNotAllowedException;
import com.sc.mapper.Vast_Mapper.DeviceGroupMapper;
import com.sc.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.service.Vast_service.DeviceGroupService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Vast
 * @version 1.0
 * @function:设备分类管理服务层实现类
 */
@Service
public class DeviceGroupServiceImpl extends ServiceImpl<DeviceGroupMapper,DeviceGroup> implements DeviceGroupService{
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;
    /**
     * 分页查询
     * @param deviceGroupPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DeviceGroupPageQueryDTO deviceGroupPageQueryDTO) {
        Page<DeviceGroup> page = new Page<>(deviceGroupPageQueryDTO.getPage(), deviceGroupPageQueryDTO.getPageSize());
        QueryWrapper<DeviceGroup> queryWrapper = new QueryWrapper<>();
        String group = deviceGroupPageQueryDTO.getGroup();
        if (group != null && !group.trim().isEmpty()) {
            queryWrapper.like("`group`", group);
        }
        Page<DeviceGroup> res = page(page, queryWrapper);

        long total=res.getTotal();
        List<DeviceGroup> records = res.getRecords();

        return new PageResult(total,records);
    }

    @Override
    //TODO 这里需要获取当前用户设置为adder
    public void save(String groupName) {
        DeviceGroup deviceGroup = DeviceGroup.builder().group(groupName).status(1).adder("王七").addTime(LocalDateTime.now()).build();
        deviceGroupMapper.insert(deviceGroup);
    }

    /**
     * 直接修改分类名称，并且更新表单剩余信息
     * @param updateGroupName
     */
    @Override
    //TODO 这里需要获取当前用户设置为adder
    public void update(String updateGroupName,Integer id) {
        QueryWrapper<DeviceGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        DeviceGroup deviceGroup = DeviceGroup.builder().group(updateGroupName).status(1).adder("王七").addTime(LocalDateTime.now()).build();
        deviceGroupMapper.update(deviceGroup,queryWrapper);
    }

    /**
     * 修改分类管理的状态
     * @param status 正常变维修，维修变正常
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Integer id) {
        QueryWrapper<DeviceGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        DeviceGroup deviceGroup = DeviceGroup.builder().status(status).build();
        deviceGroupMapper.update(deviceGroup,queryWrapper);
    }

    /**
     * 分类管理删除（包括单一删除和批量删除）
     * @param ids
     */
    @Override
    @Transactional//开启事务
    public void delete(List<Integer> ids) {
        //判断当前分类状态是否为维修中，维修中才能删除
        for (Integer id : ids) {
            DeviceGroup deviceGroup = deviceGroupMapper.selectById(id);
            if (deviceGroup.getStatus()==1){
                throw new DeletionNotAllowedException("当前设备类别存在正常运行的设备，不能随意删除");
            }
        }
        deviceGroupMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateDeviceGroup(DeviceGroupUpdateReqDTO requestParam) {
        //基本实现 业务规则不明确 数据库表设计不合理
        DeviceGroup deviceGroup = DeviceGroup.builder()
                .group(requestParam.getGroup())
                .status(requestParam.getStatus())
                .id(requestParam.getId()).build();

        boolean update = updateById(deviceGroup);

        if(!update)
        {
            throw new RuntimeException("设备分组修改失败");
        }

    }

    @Override
    public List<String> queryGroupList() {
        List<DeviceGroup> list = list();

        return list.stream().map(DeviceGroup::getGroup).collect(Collectors.toList());
    }

}